package com.xm.uml;

/**
*将Class的成员以json的数据格式展示。
*/
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class ClassToJson {
    public String convert(Class cls) throws ClassNotFoundException {
        return deepIn(cls, 1);
    }

    private String deepIn(Class cls, int depth) throws ClassNotFoundException {
        StringBuilder json = new StringBuilder("{\n");
        Field[] fields = cls.getFields();
        for (int i = 0 ; i < fields.length; i++) {
            if( i > 0){
                json.append(",\n");
            }
            json.append(appendFiledString(fields[i],depth));
        }
        json.append("\n");
        for(int i = 0; i < depth - 1; i ++){
            json.append("\t");
        }
        json.append("}");
        return json.toString();
    }

    private String appendFiledString(Field field, int deepth) throws ClassNotFoundException {
        StringBuilder filedString = new StringBuilder("");
        for(int i = 0; i < deepth; i ++){
            filedString.append("\t");
        }
        filedString.append("\"").append(field.getName()).append("\"").append(":");
        Type type = field.getGenericType();
        if(type instanceof ParameterizedType){
            filedString.append("[\"...\",\"...\",");
            Type childType = ((ParameterizedType) type).getActualTypeArguments()[0];
            if(childType.getTypeName() == Integer.class.getTypeName()||
                    childType.getTypeName() == int.class.getTypeName()||
                    childType.getTypeName() == boolean.class.getTypeName()||
                    childType.getTypeName() == String.class.getTypeName()||
                    childType.getTypeName() == Boolean.class.getTypeName()){
                filedString.append("\"...\"]");
                return filedString.toString();
            } else {
                Class childCls;
                try{
                    childCls = Class.forName(childType.getTypeName());
                } catch (ClassNotFoundException e){
                    throw e;
                }
                filedString.append(deepIn(childCls,deepth + 1));
                filedString.append("]");
                return filedString.toString();
            }
        } else {
            if(type.getTypeName() == Integer.class.getTypeName()||
                    type.getTypeName() == int.class.getTypeName()||
                    type.getTypeName() == boolean.class.getTypeName()||
                    type.getTypeName() == String.class.getTypeName()||
                    type.getTypeName() == Boolean.class.getTypeName()){
                filedString.append("null");
                return filedString.toString();
            } else {
                Class childCls;
                try{
                    childCls = Class.forName(type.getTypeName());
                } catch (ClassNotFoundException e){
                    throw e;
                }
                filedString.append(deepIn(childCls,deepth + 1));
                return filedString.toString();
            }
        }
    }
}
